{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Normalized Stacked Bar Chart\n",
    "----------------------------\n",
    "This example shows how to make a normalized stacked bar chart.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import altair as alt\n",
    "alt.data_transformers.enable('json')\n",
    "from altair.expr import datum, if_\n",
    "from vega_datasets import data\n",
    "\n",
    "source = data.population.url\n",
    "\n",
    "alt.Chart(source).mark_bar().encode(\n",
    "    alt.X('age:O', scale=alt.Scale(rangeStep=17)),\n",
    "    alt.Y('sum(people):Q',\n",
    "        axis=alt.Axis(title='population'),\n",
    "        stack='normalize'\n",
    "    ),\n",
    "    alt.Color('gender:N',\n",
    "        scale=alt.Scale(range=[\"#EA98D2\", \"#659CCA\"])\n",
    "    )\n",
    ").transform_filter(\n",
    "    datum.year == 2000\n",
    ").transform_calculate(\n",
    "    \"gender\", if_(datum.sex == 2, 'Female', 'Male')\n",
    ")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "env": {},
   "interrupt_mode": "signal",
   "language": "python",
   "metadata": {},
   "name": "python3"
  },
  "language": "python"
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
